import { mapActions,mapState } from 'vuex';
import { LOCAL_KEY } from '@/constant/dict';

export default {
    data() {
        return {
            // 是否开启漫游提示
            isRoamTips: true,

            // 轮询定时器
            roamTimer: null,

            // 轮询时间
            pollingTime: 5000,

            // 是否漫游
            isRoam: false
        };
    },

    methods: {
        ...mapActions('config', ['UPDATEROAM']),
        ...mapState('config', ['isRoamStatus']),
        async fetchRoam() {
            // const parmArr = [
            //     // 入网状态
            //     'MODEM_CEREG_STAT_GET',

            //     // 漫游状态
            //     'MODEM_ROAMING_STATE_GET'
            // ];

            // this.isRoam = ['1', '5'].includes(MODEM_CEREG_STAT_GET) && MODEM_ROAMING_STATE_GET == '1';

            // this.UPDATEROAM(this.isRoam);
        },

        pollingFetchRoam() {
            if (!this.isRoamTips) {
                return;
            }

            if (!localStorage.getItem(LOCAL_KEY.IS_AUTH_TOKEN)) {
                this.cleanRoamTimer();
                return;
            }

            this.cleanRoamTimer();

            this.fetchRoam();

            this.roamTimer = setInterval(() => {
                if (this.$store.state.globalTimerStopFlag || !localStorage.getItem(LOCAL_KEY.IS_AUTH_TOKEN)) {
                    this.cleanRoamTimer();
                    return;
                }

                this.fetchRoam();
            }, this.pollingTime);
        },

        cleanRoamTimer() {
            if (this.roamTimer) {
                clearInterval(this.roamTimer);
                this.roamTimer = null;
            }
        }
    }
};
